package com.chs.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Map;

/**
 * Created by xicheng.dong on 10/23/17.
 */
public class JavaRedisClient {
    private static int MAX_IDLE = 200;
    private static int TIMEOUT = 10000;
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool pool = null;

    public static JedisPoolConfig config() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(MAX_IDLE);
        config.setTestOnBorrow(TEST_ON_BORROW);
        return config;
    }

    public static JedisPool get() {
        if(pool == null) {
            pool = new JedisPool(config(),
                    "localhost",
                    6379,
                    TIMEOUT);
        }
        return pool;
    }

    public static Map<String, String> getAll(String key){
        Jedis jedis = JavaRedisClient.get().getResource();
        Map<String, String> stringStringMap = jedis.hgetAll(key);
        jedis.close();
        return stringStringMap;
    }
//    public static void main(String[] args){
//        Jedis jedis = JavaRedisClient.get().getResource();
//        Map<String, String> stringStringMap = jedis.hgetAll("age::money");
//        for (String key: stringStringMap.keySet()) {
//            System.out.println("key:"+key+"value:"+stringStringMap.get(key));
//        }
//    }
}
